package cn.com.lsy.product.lepao.service.impl;

import cn.com.lsy.product.lepao.model.dao.LpMessageTblMapper;
import cn.com.lsy.product.lepao.model.dao.LpUserMessageTblMapper;
import cn.com.lsy.product.lepao.model.entities.LpMessageTbl;
import cn.com.lsy.product.lepao.model.entities.LpUserMessageTblExample;
import cn.com.lsy.product.lepao.model.mt.dao.MtMessageMapper;
import cn.com.lsy.product.lepao.model.mt.entities.MessageInfoDto;
import cn.com.lsy.product.lepao.service.IJpushService;
import cn.com.lsy.product.lepao.service.IMessageService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MessageService implements IMessageService {

    private Logger log = Logger.getLogger(MessageService.class);

    @Autowired
    MtMessageMapper mtMessageMapper;
    @Autowired
    LpMessageTblMapper lpMessageTblMapper;
    @Autowired
    LpUserMessageTblMapper lpUserMessageTblMapper;
    @Autowired
    IJpushService iJpushService;

    @Override
    public List<LpMessageTbl> selectByPage(int start, int end, String startTime, String endTime, String searchCondition) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("start", start);
        where.put("end", end);
        where.put("startTime", startTime);
        where.put("endTime", endTime);
        where.put("searchCondition", searchCondition);

        return mtMessageMapper.selectByPage(where);
    }

    @Override
    public int selectByCount(String startTime, String endTime, String searchCondition) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("startTime", startTime);
        where.put("endTime", endTime);
        where.put("searchCondition", searchCondition);

        return mtMessageMapper.selectByCount(where);
    }

    @Override
    public MessageInfoDto selectMessageInfo(int id) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("id", id);

        return mtMessageMapper.selectMessageInfo(where);
    }

    @Override
    public boolean updateMessage(LpMessageTbl param) {

        if (lpMessageTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean insertUserMessage(int msgId, List<String> phoneList) {

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("msgId", msgId);

        mtMessageMapper.insertUserMessage(param);

        return false;
    }

    @Override
    public boolean insertMessage(LpMessageTbl param) {

        if (lpMessageTblMapper.insertSelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean delUserMessage(int msgId) {

        LpUserMessageTblExample example = new LpUserMessageTblExample();
        example.createCriteria().andMsgIdEqualTo(msgId);
        if (lpUserMessageTblMapper.deleteByExample(example) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean delMessage(int msgId) {

        if (lpMessageTblMapper.deleteByPrimaryKey(msgId) > 0) {

            return true;

        } else {

            return false;
        }

    }
}
